<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/8/3
 * Time: 14:04
 */

namespace app\wap\controller;


use think\Controller;

class Search extends Controller
{


    public function _initialize()
    {
        $uid=input('uid');
        $member_id=input('member_id');
        if  (!empty($uid)){
            $this->member_pcode=  $uid;
            $uids=db('member')->where('member_id',$uid)->find();
            if (!empty($member_id) && $uid != $member_id && $uids['member_pcode'] != $member_id ){
                db('member')->where('member_id',$member_id)->update([
                    'member_pcode'=> $uid
                ]);
            }
        }
    }



    public function index(){
        header('Access-Control-Allow-Origin:*');
        $shu=[];
        $search= input('search');
        if (!empty($search)){
            $data=[];
            $city_map['name']=['like','%'.$search.'%'];
            $city= db('city')
                ->where($city_map)
                ->where('deleted',0)
                ->field('name,id as city_id')
                ->limit(20)
                ->select();
                if (!empty($city)){
                    foreach ($city as $k=>$v) {
                        $v['id']='city_id';
                        $data[]=$v;
                    }
                }
            $goods_map['goods_name']=['like','%'.$search.'%'];
            $goods_map['is_group'] = 0;
            $goods= db('goods')
                ->where($goods_map)
                ->where('goods_status',1)
                ->field('goods_name as name ,goods_id')
                ->limit(20)
                ->select();
            if (!empty($goods)) {
                foreach ($goods as $k => $v) {
                    $v['id']='goods_id';
                    $data[]=$v;
                }
            }
            $line_map['title'] = ['like','%'.$search.'%'];
            $line_map['is_free'] = 0;//非自由拼线路
            $lines = db('group_tours_line')
                ->where($line_map)
                ->where(['is_deleted'=>0,'is_use'=>1])
                ->field('title as name ,line_id')
                ->limit(20)
                ->select();
            if (!empty($lines)) {
                foreach ($lines as $k => $v) {
                    $v['id']='line_id';
                    $data[]=$v;
                }
            }
            $shu = [
                'code' => 0,
                'msg' => '查询成功',
                'data' => $data
            ];

            return json_encode($shu);
        }else{
            $shu = [
                'code' => 101,
                'msg' => '请输入关键字',
            ];
            return json_encode($shu);
        }
    }


    public function popular(){
$data=[];
        $shu=[];
       $city= db('city')->where('popular',1) ->field('name,id as city_id')->select();
        if (!empty($city)){
            $data['city']=$city;

        }

        $shu = [
            'code' => 0,
            'msg' => '查询成功',
            'data' => $data
        ];

        return json_encode($shu);

    }






}